<?php

namespace Admin\Controller;

use Think\Model;

/**
 * Description of SpBookproductController
 *
 * @author Myron
 */
class SpBookproductController extends AdminController {

    /**
     * 展示门店采购列表
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function index() {
        $name = I('name');
        $shopId=I('shopId');
        $bookStatus=I('bookStatus');
        
        if($shopId>-1){
           $map['shopId'] =$shopId;
           $this->assign('shopId', $shopId);
        };
        
        if($bookStatus>-1){
           $map['bookStatus'] =$bookStatus;
           $this->assign('bookStatus', $bookStatus); 
        };
        $map['batchNum'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('sp_bookproduct', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);
        $this->meta_title = '门店订单清单';
        $this->display();
    }

    /**
     * 展示采购详情
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function detail() {
        $id = I('id');
        $BookproductData = M('sp_bookproduct')->where(array('id' => $id))->find();
        $map['bookprodId'] = $id;
        $list = M('table')->table('gr_sp_bookproddetail a,gr_sp_product b')->where('a.productId = b.id and a.bookprodId='.$id)->field('a.*,b.name as productname')->select();
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('data', $BookproductData);
        switch($BookproductData['bookStatus']){
            case 0:
                $btnStr= "审核操作";
                break; 
            case 10:
                $btnStr= "开始配货";
                break;             
            case 30:
                $btnStr= "确认发货";
                break;   
        }
        $this->assign('btnStr', $btnStr); 
        $this->display();
        
        
    }

    /**
     * 状态修改
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function changeStatus($status = '', $id='',$sendNum='',$outstockprice='',$pid='',$remark='') { 
        $gr_sp_bookproduct = array(
            'bookStatus' => $status,
            'remark'=>$remark,
        ); 
        if($status==30){
            for($i=0;$i<count($pid);$i++){
                M('sp_bookproddetail')->where(array('id' => $pid[$i]))->save(array('sendNum'=>$sendNum[$i],'subtotal'=>$outstockprice[$i]*$sendNum[$i])); 
            }
        }        
        if (!M('sp_bookproduct')->where(array('id' => $id))->save($gr_sp_bookproduct)) {
            $this->error('操作失败！');
        } else {
            $this->success('操作成功！');
        };
    }
    
    
    /******************采购订单申请******************/
    /**
     * 展示门店选择
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function addindex() {
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);     
        $this->meta_title = '门店采购清单';
        $this->display();
    }
    
    /**
     * 展示门店采购列表
     * @author 曹梦龙 <138888611@qq.com>
     */
     public function addindexshop($shopId='') {
        $name = I('name');
        $shopId=I('shopId');
        $bookStatus=I('bookStatus');
        
        if($shopId>-1){
           $map['shopId'] =$shopId;
           $this->assign('shopId', $shopId);
        };
        
        if($bookStatus>-1){
           $map['bookStatus'] =$bookStatus;
           $this->assign('bookStatus', $bookStatus); 
        };
        $map['batchNum'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('sp_bookproduct', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);
        $this->meta_title = '门店订单清单';
        $this->display(); 
     }
     
    /**
     * 展示门店采购列表
     * @author 曹梦龙 <138888611@qq.com>
     */
     function add($shopId='',$remark="",$caigouList=''){
         if(IS_POST){
            $caigouArray=json_decode($caigouList);  
            $gr_sp_bookproduct = array(
                'shopId'=>$shopId,
                'batchNum'=>NOW_TIME,
                'bookStatus'=>0,
                'createby'=>UID,
                'createat'=>NOW_TIME,
                'remark'=>$remark,
                'status'=>1,
            ); 
            $bookprodId = M('sp_bookproduct')->add($gr_sp_bookproduct);
            if (!$bookprodId) {
                     $result=array('code'=>-1,'msg'=>'订货单创建失败！');
                     $this->ajaxReturn($result);
            } else {
                    for($i=0;$i<count($caigouArray);$i++){
                        $gr_sp_bookproddetail = array(
                            'bookprodId'=>$bookprodId,
                            'productId'=>$caigouArray[$i]->pid,
                            'bookNum'=>$caigouArray[$i]->number,
                            'unit'=>'件', 
                            'outstockprice'=>$caigouArray[$i]->price,
                            'subtotal'=>$caigouArray[$i]->amountprice,
                            'status'=>1,
                        ); 
                        M('sp_bookproddetail')->add($gr_sp_bookproddetail);
                    }
                    $result=array('code'=>1,'msg'=>'订货单创建成功！');
                    $this->ajaxReturn($result);
            };
            
         }else{
             $map['status']=1;
             $pcategorylist = M('sp_pcategory')->where($map)->order('type')->select(); 
             int_to_string($pcategorylist);
             $this->assign('_pcategorylist', $pcategorylist);
             $this->assign('shopId', $shopId);
             $this->display();
         } 
     }
     
     
     
  
    /********Ajax****/
    /*
     * 获取分类下的商品列表
     */
    function getProductListAjax($cid=''){
        $productList=M('sp_product')->where(array('pCategoryId'=>$cid))->select();
        for($i=0;$i<count($productList);$i++){
            $productList[$i]['cnt']=get_vinventory_count($productList[$i]['code']);
        }
        $this->ajaxReturn($productList);
    }

    /********Ajax****/
    /*
     * 订货单创建后看详情
     */
    function adddetail($id=''){
          $id = I('id');
        $BookproductData = M('sp_bookproduct')->where(array('id' => $id))->find();
        $map['bookprodId'] = $id;
        $list = M('table')->table('gr_sp_bookproddetail a,gr_sp_product b')->where('a.productId = b.id and a.bookprodId='.$id)->field('a.*,b.name as productname')->select();
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('data', $BookproductData);
        switch($BookproductData['bookStatus']){
            case 0:
                $btnStr= "审核操作";
                break; 
            case 10:
                $btnStr= "开始配货";
                break;             
            case 30:
                $btnStr= "确认发货";
                break;   
        }
        $this->assign('btnStr', $btnStr); 
        $this->display();
        
    }
    
    /*
     * 打印预览
     */
    public function printf($id=''){
        $id = I('id');
        $BookproductData = M('sp_bookproduct')->where(array('id' => $id))->find();       
        $map['bookprodId'] = $id;
        $list = M('table')->table('gr_sp_bookproddetail a,gr_sp_product b')->where('a.productId = b.id and a.bookprodId='.$id)->field('a.*,b.name as productname')->select();
        int_to_string($list);
        $sumshuliang= M('sp_bookproddetail')->where(array('bookprodId' => $id))->Sum('sendNum'); 
        $sumjine= M('sp_bookproddetail')->where(array('bookprodId' => $id))->Sum('subtotal');       
        $this->assign('_list', $list);
        $this->assign('data', $BookproductData);
        $this->assign('sumshuliang', $sumshuliang);
        $this->assign('sumjine', $sumjine);
         $this->assign('nowtime', time_format(NOW_TIME));
        $this->display();
    }
}
